import sys,os

if len(sys.argv)<2:
    print 'Usage: python extractTable.py tableList'
    exit(1)

type=sys.argv[1].split('.')[0]
i=0
colName=['chrom','chromStart','chromEnd','name','score','strand','thickStart','thickEnd','reserved','blockCount','blockSizes','chromStarts']
fi=open(sys.argv[1])
for line in fi:
    line=line.strip()
    if len(line)<1:
    	continue
#    temp=line.split('\t')
#    cmd='mysql -uroot hg19 -N -e \"select chrom,chromStart,chromEnd,name,score,strand,signalValue,pValue,qValue from '+name+';\" >'+type+'/'+name+'.'+type

#    if len(temp[1].split(' '))<2:
#	print 'not bed: '+temp[0]
#	continue
#    n_col=int(temp[1].split(' ')[1])
#    if n_col==6 or n_col==12 or n_col>12:
#    	continue
#    cmd='mysql -uroot hg19 -N -e \"select '
#    for i in range(n_col):
#	if i==0:
#	    cmd+=colName[i]
#	else:
#	    cmd+=','+colName[i]
#    cmd+=' from '+temp[0]+';\" >'+type+'/'+temp[0]+'.'+type

#    if 'bed 6' in temp[1]:
#    	cmd='mysql -uroot hg19 -N -e \"select chrom,chromStart,chromEnd,name,score,strand from '+temp[0]+';\" >'+type+'/'+temp[0]+'.'+type
#    elif 'bed 12' in temp[1]:
#    	cmd='mysql -uroot hg19 -N -e \"select chrom,chromStart,chromEnd,name,score,strand,thickStart,thickEnd,reserved,blockCount,blockSizes,chromStarts from '+temp[0]+';\" >'+type+'/'+temp[0]+'.'+type
#    else:
#    	continue

    if 'pubs' in line:
    	cmd='mysql -uroot hg19 -N -e \"select chrom,chromStart,chromEnd,name,matchCount from '+line+';\" >bed/'+line+'.bed'
    elif 'dgv' in line:
    	cmd='mysql -uroot hg19 -N -e \"select chrom,chromStart,chromEnd,name,score,strand,thickStart,thickEnd,itemRgb from '+line+';\" >bed/'+line+'.bed'
    else:
	cmd='mysql -uroot hg19 -N -e \"select chrom,chromStart,chromEnd,name,score,strand,thickStart,thickEnd,color from '+line+';\" >bed/'+line+'.bed'
    #print cmd
    os.system(cmd)
    #if i>5:
    #	break
fi.close()
